#!/usr/bin/env node

const Router = require('koa-router'),
	business = require('../models/business_mod'),
	image = require('../models/image_mod'),
	router = new Router;
router.prefix('/api/business');
router.get('/', async (ctx, next) => {
	let res = await business.Get();
	ctx.body = res;
})
router.post('/register', async (ctx, next) => {
	let { business_phone, business_password, password_confim } = ctx.request.body;
	let res = await business.Business_register(business_phone, business_password, password_confim);
	ctx.body = res;
})
router.post('/', async (ctx, next) => {
	let { business_phone, business_password } = ctx.request.body;
	let res = await business.Login(business_phone, business_password);
	ctx.body = res;
})
router.put('/', async (ctx, next) => {
	console.log(ctx.request.body);
	let filename = '';
	let filename1 ='';
	if (ctx.request.body.File == undefined) {
		const file = ctx.request.files.File;
		filename = await image.Image(file);
	} else {
		let fileurl = ctx.request.body.File.split('/')
		filename = fileurl[fileurl.length - 1]
	}
	let {business_id, business_name, business_phone, business_email, business_password, password_confim, business_header, county_id, business_time, business_describe, password,business_address} = ctx.request.body;
	console.log(business_address);
	let res = await business.Modify(business_id, business_name, filename,filename1, business_phone, business_email, business_password, password_confim, business_header, county_id, business_time, business_describe, password,business_address);
	ctx.body = res;
})

router.put('/2', async (ctx, next) => {
	let filename = '';
	let filename1 = '';
	console.log(ctx.request.body.File1);
	if (ctx.request.body.File1 == undefined) {
		const file = ctx.request.files.File1;
		filename1 = await image.Image(file);
	} else {
		let fileurl = ctx.request.body.File1.split('/')
		filename1 = fileurl[fileurl.length - 1]
	}
	let { business_id, business_name, business_phone, business_email, business_password, password_confim, business_header, county_id, business_time, business_describe, password ,business_address} = ctx.request.body;
	let res = await business.Modify(business_id, business_name,filename ,filename1, business_phone, business_email, business_password, password_confim, business_header, county_id, business_time, business_describe, password,business_address);
	ctx.body = res;
})
router.put('/3', async (ctx, next) => {
	console.log(ctx.request.body);
        let filename1 = '';
	let filename = '';
        let { business_id, business_name, business_phone, business_email, business_password, password_confim, business_header, county_id, business_time, business_describe, password,business_address } = ctx.request.body;
        let res = await business.Modify(business_id, business_name,filename,filename1, business_phone, business_email, business_password, password_confim, business_header, county_id, business_time, business_describe, password,business_address);
        ctx.body = res;
})
router.delete('/', async (ctx, next) => {
	let { business_id, admin_id, business_password, admin_password } = ctx.request.body;
	let res = await business.Business_Delete(business_id, admin_id, business_password, admin_password);
	ctx.body = res;
})
router.post('/commodity', async (ctx, next) => {
	let { business_id } = ctx.request.body;
	let res = await business.get_C(business_id);
	ctx.body = res;
});
router.get('/commodity', async (ctx, next) => {
	let res = await business.get_A();
	ctx.body = res;
})
router.get('/business', async (ctx, next) => {
	let res = await business.Look();
	ctx.body = res;
})
module.exports = router
